/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package unplag.engine;

import unplag.core.Engine;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 *
 * @author Administrator
 */
public class EntropyEngine implements Engine{
    public long compress(String s1,String s2){
        String a;
        ZipOutputStream zipos=new ZipOutputStream(new ByteArrayOutputStream());
        zipos.setLevel(9);
        zipos.setMethod(ZipOutputStream.DEFLATED);
        ZipEntry ze=new ZipEntry("Test");

        byte[] b1=s1.getBytes();
        byte[] b2=s2.getBytes();

        try {
            zipos.putNextEntry(ze);
            zipos.write(b1);
            zipos.write(b2);
            zipos.closeEntry();
            return ze.getCompressedSize();
        } catch (IOException ex) {
            Logger.getLogger(EntropyEngine.class.getName()).log(Level.SEVERE, null, ex);
        }
        return 0;
    }

    @Override
    public double compare(String s1, String s2) {
        long l1=compress(s1,s1);
        long l2=compress(s2,s2);
        long l=compress(s1,s2);
        //return Math.min((double)l/l1, (double)l/l2);
        return (double)(l1+l2-l)/l;
    }
}
